package com.ali.juejin.ui.activity

import android.animation.Animator
import android.graphics.Color
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.core.widget.NestedScrollView
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.ali.juejin.BR
import com.ali.juejin.R
import com.ali.juejin.adapter.ArticleCommentListAdapter
import com.ali.juejin.adapter.ArticleMoreAdapter
import com.ali.juejin.base.BaseModelActivity
import com.ali.juejin.bean.article.Comment
import com.ali.juejin.bean.v1.article.comment.Data
import com.ali.juejin.databinding.ActivityArticleDetailBinding
import com.ali.juejin.databinding.ItemArticleCommentBinding
import com.ali.juejin.databinding.ItemArticleMoreBinding
import com.ali.juejin.model.ArticleDetailModel
import com.blankj.utilcode.util.ConvertUtils
import kotlinx.android.synthetic.main.activity_article_detail.*

class ArticleDetailActivity: BaseModelActivity() {

    var titleAnimate:Boolean = true

    val model by lazy { ViewModelProvider(this).get(ArticleDetailModel::class.java) }
    val articleMoreAdapter by lazy { ArticleMoreAdapter<ItemArticleMoreBinding,ArticleDetailActivity>(this) }
    val articleCommentListAdapter by lazy { ArticleCommentListAdapter<ItemArticleCommentBinding,Data>(this,BR.articleComment) }

    override fun initView() {
        val binding = DataBindingUtil.setContentView<ActivityArticleDetailBinding>(this, R.layout.activity_article_detail)
        binding.articleDetail = model
        binding.lifecycleOwner = this

        // 标题动画切换
        article_scroll.setOnScrollChangeListener { mView: NestedScrollView?, _: Int, scrollY: Int, _: Int, _: Int ->
//            LogUtils.wTag("滑动距离",scrollY)
//            LogUtils.wTag("顶部距离",ConvertUtils.dp2px(66f))
            if(scrollY>ConvertUtils.dp2px(66f) && titleAnimate){
                // 向下滑动
                // 文字向上，只显示头像
//                LogUtils.wTag("向下滑动",scrollY-oldScrollY)
                at1.animate().translationY(-150f).setListener(object :Animator.AnimatorListener{

                    override fun onAnimationRepeat(p0: Animator?) {
                    }

                    override fun onAnimationEnd(p0: Animator?) {

                    }

                    override fun onAnimationCancel(p0: Animator?) {
                    }

                    override fun onAnimationStart(p0: Animator?) {
                        titleAnimate = false

                    }

                }).start()

                at2.animate().translationY(0f).setListener(object :Animator.AnimatorListener{
                    override fun onAnimationRepeat(p0: Animator?) {
                    }

                    override fun onAnimationEnd(p0: Animator?) {
                        titleAnimate = true
                    }

                    override fun onAnimationCancel(p0: Animator?) {
                    }

                    override fun onAnimationStart(p0: Animator?) {
                    }

                }).start()
            }else if((scrollY<ConvertUtils.dp2px(66f) && titleAnimate) || scrollY==0){
                // 向上滑动
                // 头像向下，只显示文字
                at2.animate().translationY(150f).setListener(object :Animator.AnimatorListener{

                    override fun onAnimationRepeat(p0: Animator?) {
                    }

                    override fun onAnimationEnd(p0: Animator?) {

                    }

                    override fun onAnimationCancel(p0: Animator?) {
                    }

                    override fun onAnimationStart(p0: Animator?) {
                        titleAnimate = false
                    }

                }).start()

                at1.animate().translationY(0f).setListener(object :Animator.AnimatorListener{
                    override fun onAnimationRepeat(p0: Animator?) {
                    }

                    override fun onAnimationEnd(p0: Animator?) {
                        titleAnimate = true
                    }

                    override fun onAnimationCancel(p0: Animator?) {
                    }

                    override fun onAnimationStart(p0: Animator?) {
                    }

                }).start()
            }

            // NestedScrollView滑动到底，加载更多
            if(mView?.getChildAt(0)!!.height <= scrollY + mView.height){
//                LogUtils.wTag("NestedScrollView滑动到底","加载更多")
                loadMoreComment()
            }
        }

        // 返回
        detail_back.setOnClickListener {
            finish()
        }

        article_more.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)
        article_more.adapter = articleMoreAdapter

        article_comment_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)
        article_comment_list.adapter = articleCommentListAdapter

    }

    override fun initData() {
        val entryIds = intent.getStringExtra("id")
        val tag_ids = intent.getStringArrayListExtra("tag_ids")
        val user_id = intent.getStringExtra("user_id")
        entryIds?.let {
            model.getDetailInfo(entryIds)
            model.getRelatedEntry(entryIds,tag_ids,user_id)
            model.getCommentList(entryIds)
        }

        model.relatedEntryList.observe(this, Observer {
            newValue->
            if(newValue.size > 3){
                articleMoreAdapter.fresh(arrayListOf(newValue.get(0),newValue.get(1),newValue.get(2)))
            }else{
                articleMoreAdapter.fresh(newValue)
            }

        })

        model.detailInfo.observe(this, Observer {
            for(tag in it.tags){
                val tagView = TextView(this)
                val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                tagView.setText(tag.tag_name)
                tagView.setPadding(ConvertUtils.dp2px(10f))
                layoutParams.setMargins(ConvertUtils.dp2px(20f),0,0,0)
                tagView.setTextColor(Color.parseColor("#9FA3AA"))
                tagView.setBackgroundColor(Color.parseColor("#EBEBEB"))
                tagView.layoutParams = layoutParams
                article_detail_tag.addView(tagView)
            }
        })

        model.commentList.observe(this, Observer {
            newValue->
            if(newValue.data.size >0)articleCommentListAdapter.load(newValue.data)
        })

    }

    // 加载更多评论
    fun loadMoreComment(){
        val list = model.commentList.value
        list?.let {
            if(list.data.size>0) model.getCommentList(intent.getStringExtra("id")!!)
        }
    }

}